<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sis_setup_parameter extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('sis_setup_variabel_model', 'Variabel');
		$this->load->model('sis_setup_parameter_model', 'Parameter');
		$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	private function _get_variabel(){
		$arr_field = array();
		if(get('param') == 'simpanan'){
			$arr_field	= array('POKOK', 'WAJIB');
		}else if(get('param') == 'bunga'){
			$arr_field	= array('BUNGA_SAVING','BUNGA_DEPOSITO','BUNGA_PINJAM_UANG','BUNGA_PINJAM_BARANG','BUNGA_PINJAM_LAIN');
		}else if(get('param') == 'saldo'){
			$arr_field	= array('SALDO_MIN');
		}else if(get('param') == 'biaya'){
			$arr_field	= array(/*'BIAYA_ADMIN',*/'BIAYA_JK.0000001','BIAYA_JK.0000002','BIAYA_JK.0000003');
		}
		return $arr_field;
	}
	
	function index(){
		$data['title'] = $this->session_menu_label;
		
		$data['arr_jenis_anggota']	= $this->Parameter->GetAllJenisAnggota();
		$data['arr_variabel']		= $this->Variabel->GetDetail($this->_get_variabel());
		
		viewByTemplate('sis_setup_parameter/index', $data, 'grid');
	}
	
	function getdata()
	{
		// Ajax Response For Datagrid
		echo $this->Parameter->GetJSON($this->_get_variabel());
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		if (post("tgl_efektif") == '') {
			$error->Add("tgl_efektif", "Tgl Efektif", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM SIS_SETUP_PARAMETER WHERE TGL_EFEKTIF = TO_DATE('".post("tgl_efektif")."','DD/MM/YYYY') AND ID_VARIABEL IN ('".implode("','",$this->_get_variabel())."')")) > 0) {
			$error->Add("tgl_efektif", "Tgl Efektif", $tpl_duplicate);
		}
		
		/*$arr_variabel = $this->Variabel->GetDetail($this->_get_variabel());
		for($i=0,$max_i = count($arr_variabel); $i<$max_i; $i++){
			$row = $arr_variabel[$i];
			
			$id_variabel	= strtolower($row['id_variabel']);
			if (post($id_variabel) == '' || (toNumber(post($id_variabel)) <= 0 && (substr($id_variabel,0,5)!='bunga'))) {
				$error->Add($id_variabel, $row['nama_variabel'], $tpl_empty);
			}
		}*/

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";
		
		// Get Error Template
		$tpl_insert_success = $this->MessageTemplate("CS001");
		$tpl_update_success = $this->MessageTemplate("CS002");
		
		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('tgl_efektif'));
				
				// Set Date Format
				$data['tgl_efektif']	= ($data['tgl_efektif'] == '' ? NULL : date_format3($data['tgl_efektif'], 'YYYY-MM-DD'));
				
				$arr_jenis_anggota	= $this->Parameter->GetAllJenisAnggota();
				$arr_variabel		= $this->Variabel->GetDetail($this->_get_variabel());
				
				for($j=0, $max_j=count($arr_jenis_anggota); $j<$max_j; $j++){
					$row_jnsagt = $arr_jenis_anggota[$j];
					$key	= strtolower($row_jnsagt['kd_jenis_anggota']); 
					
					for($i=0,$max_i = count($arr_variabel); $i<$max_i; $i++){
						$row = $arr_variabel[$i];
						
						$id_variabel	= strtolower(str_replace('.','_',$row['id_variabel']));
						
						if($this->Parameter->GetSingle($row['id_variabel'], $row_jnsagt['kd_jenis_anggota'], $data['tgl_efektif']) == NULL){
							$this->Parameter->Add(	array(	'ID_VARIABEL'	=> $row['id_variabel'],
															'TGL_EFEKTIF'	=> $data['tgl_efektif'],
															'KD_JENIS_ANGGOTA' => $row_jnsagt['kd_jenis_anggota'],
															'NILAI'			=> (int)toNumber(post($id_variabel.'_'.$key)),
															'CREATE_USER'	=> USERID,
															'CREATE_DATE'	=> NOW));
							
							$ls_response = $tpl_insert_success['message_id'];
						}else{
							$this->Parameter->Update(array(	'ID_VARIABEL'	=> $row['id_variabel'],
															'TGL_EFEKTIF'	=> $data['tgl_efektif'],
															'NILAI'			=> (int)toNumber(post($id_variabel.'_'.$key)),
															'MODIFY_USER'	=> USERID,
															'MODIFY_DATE'	=> NOW),
													 array(	'ID_VARIABEL'	=> $row['id_variabel'],
															'TGL_EFEKTIF'	=> $data['tgl_efektif'],
															'KD_JENIS_ANGGOTA' => $row_jnsagt['kd_jenis_anggota']));
							$ls_response = $tpl_update_success['message_id'];
						}
					}
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$tgl_efektif	= get('tgl_efektif');
		
		if ($tgl_efektif != "")
		{
			// Delete Single
			$arr_variable = $this->_get_variabel();
			for($i=0,$max_i=count($arr_variable); $i<$max_i; $i++){
				$arr_rule	= array('tgl_efektif' => $tgl_efektif, 'id_variabel' => $arr_variable[$i]);
				$ls_result = $this->Parameter->DeleteSingle($arr_rule);
			}
		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_variable = explode('|', $row['param']);
				for($i=0,$max_i=count($arr_variable); $i<$max_i; $i++){
					$arr_rule	= array('tgl_efektif' => date_format3($row['tgl_efektif'],'YYYY-MM-DD'), 'id_variabel' => $arr_variable[$i]);
					$ls_result .= $this->Parameter->DeleteSingle($arr_rule);
				}
			}
		}
		
		echo $ls_result;
	}
}
?>